const db = require('../db/index');
const myDate = require('../encapsulation/tool/myDate');

// 查询员工流动数据
exports.getUserData = (req, res) => {
	// 从部门考勤表里拿到对应年份得数据 每天/各部门总人数
	const sql = 'select * from department_attendance where year(day_date)=?';
	db.query(sql, req.query.year, (err, results) => {
		if (err) return res.cc(err);
		let data = results;
		// 从部门表里拿到每个未删除部门的id
		const sql = 'select * from department1 where delete_state=0 and state=0';
		db.query(sql, (err, results) => {
			if (err) return res.cc(err);
			let arr = [];
			let index = 0;
			for (let j = 0; j < 12; j++) {
				index++;
				if (index < 10) {
					index = '0' + index
				}
				arr[j] = [];
				for (let k = 0; k < results.length; k++){
					arr[j][k] = {day_date: 'x', department_id: 'x'}
					arr[j][k].day_date = req.query.year + '-' + index
					arr[j][k].department_id = results[k].department1_id
					arr[j][k].department_name = results[k].department1
				}
			}
			index = 0;
			for (let i = 0; i < arr.length; i++) {
				for (let k = 0; k < arr[i].length; k++) {
					let total_number = 0;
					for (let j = 0; j < data.length; j++) {
						if (arr[i][k].day_date === data[j].day_date.slice(0, 7) && arr[i][k].department_id === data[j].department_id) {
							total_number = total_number + data[j].total_number;
							index++;
						}
					}
					if(total_number === 0){
						arr[i][k].total_number = 0;
						index = 0;
					} else {
						total_number = total_number / index;
						arr[i][k].total_number = total_number.toFixed(2);
						index = 0;
					}
				}
			}
			res.send({
				status: 0,
				message: '查询成功',
				data: arr
			})
		})
	})
}

// 考勤人数(全公司，部门1，部门2)
exports.getAttendanceData = (req, res) => {
	if (req.query.department_id != 0){
		const sql = 'select * from department_attendance where day_date=? and department_id=?';
		db.query(sql, [req.query.day_date, req.query.department_id], (err, results) => {
			if (err) return res.cc(err);
			console.log(results)
			res.send({
				status: 0,
				message: '查询成功',
				data: results
			})
		})
	} else {
		const sql = 'select * from department_attendance where day_date=?';
		db.query(sql, req.query.day_date, (err, results) => {
			if (err) return res.cc(err);
			let obj = {
				office_number: 0,
				rest_number: 0,
			};
			for(let i = 0 ; i < results.length ; i++) {
				obj.office_number = obj.office_number + results[i].office_number;
				obj.rest_number = obj.rest_number + results[i].rest_number
			}
			obj.total_number = obj.office_number + obj.rest_number;
			res.send({
				status: 0,
				message: '查询成功',
				data: obj
			})
		})
	}
}

// 查询项目
exports.getProjectData = (req, res) => {
	let obj = {
		underway_project: 0,
		end_project: 0,
		await_project: 0
	};
	let toDay = myDate.toDay();
	// 查询进行中项目
	const sql = 'select * from project where project_end_date>? and project_start_date<?';
	db.query(sql, [toDay, toDay], (err, results) => {
		if (err) return res.cc(err);
		obj.underway_project = results.length;
		// 查询结束的项目
		const sql = 'select * from project where project_end_date<?';
		db.query(sql, toDay, (err, results) => {
			if (err) return res.cc(err);
			obj.end_project = results.length;
			// 查询待开始的项目
			const sql = 'select * from project where project_start_date>?';
			db.query(sql, toDay, (err, results) => {
				if (err) return res.cc(err);
				obj.await_project = results.length;
				res.send({
					status: 0,
					message: '查询成功',
					data: obj
				})
			})
		})
	})
}

// 查询工资
exports.getSalaryData = (req, res) => {
	const sql = 'select * from salary';
	db.query(sql, (err, results) => {
		if (err) return res.cc(err);
		let arr = [];
		let index = 0;
		for (let i = 0 ; i < 12; i++) {
			arr[i] = {};
			arr[i].all_salary = 0;
			index++;
			if (index < 10) {
				index = '0' + index
			}
			arr[i].s_date = req.query.year + '-' + index
			for(let j = 0 ; j < results.length ; j++){
				if(results[j].s_date.slice(0,4) == req.query.year && results[j].s_audit_state === 2 && results[j].s_date.slice(5,7) == index){
					arr[i].all_salary = arr[i].all_salary + results[j].s_practical_salary
				}
			}
		}
		res.send({
			status: 0,
			message: '查询成功',
			data: arr
		})
	})
}